{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8adb89ba-532c-4187-99bc-a3f69a951539",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "3baceedc-9e7d-49cf-9aa3-0965ec53991a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>30</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>40</td>\n",
       "      <td>120</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>88</td>\n",
       "      <td>150</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A   B    C\n",
       "0  1  20   90\n",
       "1  1  30  100\n",
       "2  1  40  120\n",
       "3  2  88  150"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# %%capture outputs\n",
    "df = pd.DataFrame({\n",
    "    \"A\": [1, 1, 1, 2],\n",
    "    \"B\": [20, 30, 40, 88],\n",
    "    \"C\": [90, 100, 120, 150]\n",
    "})\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "3aee1ab6-e9be-4046-9680-e38fbfa050d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "g = df.groupby(\"A\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "7eafe1bd-92ea-494c-97d6-f95013022af1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>30</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>40</td>\n",
       "      <td>120</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A   B    C\n",
       "0  1  20   90\n",
       "1  1  30  100\n",
       "2  1  40  120"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g.get_group(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "dbf5c15b-c51b-4fb7-a904-4d4f50c15a59",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20/30/40</td>\n",
       "      <td>90/100/120</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>88</td>\n",
       "      <td>150</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          B           C\n",
       "A                      \n",
       "1  20/30/40  90/100/120\n",
       "2        88         150"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = df.groupby(by=\"A\").apply(\n",
    "    lambda x: pd.Series({\n",
    "        \"B\":\"/\".join([str(v) for v in x[\"B\"]]),\n",
    "        \"C\":\"/\".join([str(v) for v in x[\"C\"]])\n",
    "    })\n",
    ")\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "dd4228b5-7ea3-4e9e-a1d4-448821b78655",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    [1, 2, 3]\n",
       "b    [2, 3, 4]\n",
       "dtype: object"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Series({\"a\":[1,2,3], \"b\":[2,3,4]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "61f5df53-d204-44af-8dbf-e9130c74bb3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "g = df.groupby(\"A\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "2ce4431c-f327-45ca-b7e0-3e3cc674f694",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0.333333\n",
       "1    0.333333\n",
       "2    0.333333\n",
       "3    1.000000\n",
       "Name: A, dtype: float64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g[\"A\"].apply(lambda x: x/x.sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c54e5b45-f939-478a-a433-7bb1a8f72a65",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "fd131b0cbadea90dff19ab717cdf3ccb31cb4d300e98013e350b223a65895fcf"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
